<!--
To change this template, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Film page</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript" src="common.js"></script>
    <script type="text/javascript">

        // Initialize the producer select box
        function initProducerSelect() {
            removeAllSelectOptions("producer_code");
            addSelectOptions("producer_code", "Choose one", "producer");
            ajaxCall('getproducers.php', loadProducersFromXml);
        }

        // Parse the XML file containing the list of producers and add
        // to the producer select box
        function loadProducersFromXml(xml_doc) {
            xmlDoc=loadXMLString(xml_doc);
            
            prod_list=xmlDoc.documentElement.getElementsByTagName("producer");
            for (i=0;i<prod_list.length;i++) {
                prod_code=prod_list[i].getElementsByTagName("code");
                prod_name=prod_list[i].getElementsByTagName("name");
                try {
                    code = prod_code[0].firstChild.nodeValue;
                    name = prod_name[0].firstChild.nodeValue;
                    addSelectOptions("producer_code", code, name);
                } catch (er) {
                    code = 1;
                    name = '';
                }
            }

        }

        function saveRespHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            switch (result_text.toLowerCase()) {
                case "added":
                    alert("Film added!")
                    break;
                case "updated":
                    alert("Film updated");
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the save response");
                    break;
            }
        }

        function queryRespHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            switch (result_text.toLowerCase()) {
                case "deleted":
                    alert('Deleted film');
                    resetForm();
                    break;
                case "success":
                    //alert('Successful query');
                    try {
                        film_name=xmlDoc.getElementsByTagName("film_name");
                        document.getElementById("film_name").value=film_name[0].firstChild.nodeValue;
                        film_code=xmlDoc.getElementsByTagName("film_code");
                        document.getElementById("film_code").value=film_code[0].firstChild.nodeValue;
                        total_no_tickets=xmlDoc.getElementsByTagName("total_no_tickets");
                        document.getElementById("total_no_tickets").value=total_no_tickets[0].firstChild.nodeValue;
                        sold_no_tickets=xmlDoc.getElementsByTagName("sold_no_tickets");
                        document.getElementById("sold_no_tickets").value=sold_no_tickets[0].firstChild.nodeValue;
                        producer_code=xmlDoc.getElementsByTagName("producer_code");
                        //initSelect("producer_code");
                        selectOptionValue("producer_code", producer_code[0].firstChild.nodeValue);
                    } catch (er) {
                        alert('Invalid film code');
                    }
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the query response");
                    break;
            }
        }

        function dispTableHdlr(xml_doc) {
            //TODO: Handle the response from the server for the save command
            xmlDoc=loadXMLString(xml_doc);

            result=xmlDoc.getElementsByTagName("result");
            try {
                result_text = result[0].firstChild.nodeValue;
            } catch (er) {
                result_text = "Error";
            }
            table_text = "<button onclick='displayTable()'>Display the film table</button>";
            table_text = table_text + "<table border='1'>"+
                      "<tr><th>ID</th>"+
                          "<th>Film Name</th>"+
                          "<th>Film Code</th>"+
                          "<th>Total number of tickets</th>"+
                          "<th>Sold number of tickets</th>"+
                          "<th>Producer Code</th></tr>";

            switch (result_text.toLowerCase()) {
                case "success":
                    //alert('Successful query');
                    films=xmlDoc.getElementsByTagName("film");
                    for (i=0;i<films.length;i++) {
                        table_text = table_text + "<tr>";
                        try {
                            table_text = table_text + insertField(films[i], "film_id");
                            table_text = table_text + insertField(films[i], "film_name");
                            table_text = table_text + insertField(films[i], "film_code");
                            table_text = table_text + insertField(films[i], "total_no_tickets");
                            table_text = table_text + insertField(films[i], "sold_no_tickets");
                            table_text = table_text + insertField(films[i], "producer_code");
                        } catch (er) {
                            alert('Invalid film code');
                        }
                        table_text = table_text + "</tr>";
                    }
                    break;
                case "login":
                    alert("Login required. You will now be redirected to the login page");
                    window.location="login.html";
                    break;
                case "error":
                default:
                    alert("Error occured while processing the query response");
                    break;
            }

            table_text = table_text + "</table>";
            document.getElementById("film_table").innerHTML=table_text;
        }

        function validateForm() {
            var return_value = true;
            var film_name=document.getElementById("film_name").value;
            var film_code=document.getElementById("film_code").value;
            var total_no_tickets=document.getElementById("total_no_tickets").value;
            var sold_no_tickets=document.getElementById("sold_no_tickets").value;
            var producers=document.getElementById("producer_code");

            if (!film_name) {
                document.getElementById("film_name_hint").innerHTML =
                    "<p style='color:red'><b>Name cannot be empty </b></p>";
                return_value = false;
            } else {
                document.getElementById("film_name_hint").innerHTML = " ";
            }

            if (!film_code) {
                document.getElementById("film_code_hint").innerHTML =
                    "<p style='color:red'><b>Code cannot be empty </b></p>";
                return_value = false;
            } else {
                document.getElementById("film_code_hint").innerHTML = " ";
            }

            if (!total_no_tickets) {
                document.getElementById("total_hint").innerHTML =
                    "<p style='color:red'><b>Total number of tickets cannot be empty </b></p>";
                return_value = false;
            } else {
                if (isNaN(total_no_tickets)) {
                    document.getElementById("total_hint").innerHTML =
                        "<p style='color:red'><b>Total number of tickets must be a number </b></p>";
                    return_value = false;
                } else {
                    document.getElementById("total_hint").innerHTML = " ";
                }
            }

            if (!producers.selectedIndex) {
                document.getElementById("producer_code_hint").innerHTML =
                    "<p style='color:red'><b>Producer Code should be selected</b></p>";
                return_value = false;
            } else {
                document.getElementById("producer_code_hint").innerHTML = " ";
            }

            return return_value;

        }

        function saveForm() {
            if (!validateForm()) {
                alert('Correct errors in the form before trying again!');
                return;
            }

            var film_name=document.getElementById("film_name").value;
            var film_code=document.getElementById("film_code").value;
            var total_no_tickets=document.getElementById("total_no_tickets").value;
            var sold_no_tickets=document.getElementById("sold_no_tickets").value;
            var producers=document.getElementById("producer_code");
            var selected_producer=producers.options[producers.selectedIndex].value;

            srvr_request="savefilm.php?film_name="+film_name+
                            "&film_code="+film_code+
                            "&total_no_tickets="+total_no_tickets+
                            "&sold_no_tickets="+sold_no_tickets+
                            "&producer_code="+selected_producer;
                        
            ajaxCall(srvr_request,saveRespHdlr);
        }

        function resetForm() {
            document.getElementById("film_name").value = "";
            document.getElementById("film_name_hint").innerHTML = "";
            document.getElementById("film_code").value = "";
            document.getElementById("film_code_hint").innerHTML = "";
            document.getElementById("total_no_tickets").value = "";
            document.getElementById("total_hint").innerHTML = "";
            document.getElementById("sold_no_tickets").value = "";
            document.getElementById("sold_hint").innerHTML = "";
            initProducerSelect();
            document.getElementById("producer_code_hint").innerHTML = "";
        }

        function queryForm() {
            var film_code=document.getElementById("film_code").value;
            srvr_request="queryfilm.php?film_code="+film_code;
            ajaxCall(srvr_request,queryRespHdlr);
        }

        function deleteForm() {
            var film_code=document.getElementById("film_code").value;
            srvr_request="queryfilm.php?film_code="+film_code+
                        "&action=delete";
            ajaxCall(srvr_request,queryRespHdlr);
        }

        function displayTable() {
            srvr_request="queryfilm.php";
            ajaxCall(srvr_request,dispTableHdlr);
        }

        function checkLoginCookie() {
            session_auth=getCookie('session_auth');
            logged_username=getCookie('username');

            if(session_auth!='kteam_auth' || !logged_username) {
                setCookie('redirector','film.html',60*60*1000);
                window.location = "login.html";
            }
            initProducerSelect();
        }


    </script>

  </head>
  <body onLoad="checkLoginCookie()">
      <table border="1">
          <tr><td width="150"><a href="producer.html"><b>Producers</b></a></td>
              <td width="150"><a href="film.html"><b>Films</b></a></td>
              <td width="150"><a href="centre.html"><b>Centres</b></a></td>
              <td width="150"><a href="agent.html"><b>Agents</b></a></td>
              <td width="150"><a href="customer.html"><b>Tickets</b></a></td>
              <td width="150"><a href="ticket.html"><b>Ticket rate information</b></a></td>
              <td width="150"><a href="ticketstatus.html"><b>Status of ticket sales for each film</b></a></td>
              <td width="150"><a href="customerstatus.html"><b>Status for each customer</b></a></td>
              <td width="150"><a href="logout.php"><b>Logout</b></a></td></tr>
      </table>

        <table border="0">

            <tr><td><label>Film Name&nbsp;*</label></td>
                <td><input id="film_name" type=text name=film_name size='20' value=''></td>
                <td><div id="film_name_hint"> </div></td></tr>

            <tr><td><label>Film Code&nbsp;*</label></td>
                <td><input id="film_code" type=text name=film_code size='20' value=''></td>
                <td><div id="film_code_hint"> </div></td></tr>

            <tr><td><label>Total number of tickets&nbsp;*</label></td>
                <td><input id="total_no_tickets" type=text name=total_no_tickets size='20' value=''></td>
                <td><div id="total_hint"> </div></td></tr>

            <tr><td><label>Number of tickets Sold</label></td>
                <td><input id="sold_no_tickets" type=text name=sold_no_tickets size='20' value=''></td>
                <td><div id="sold_hint"> </div></td></tr>

            <tr><td><label>Producer&nbsp;*</label></td>
                <td><select id="producer_code" name=producer_code><option value=''></option></select></td>
                <td><div id="producer_code_hint"> </div></td></tr>

            <tr /> <tr /> <tr />
        </table>
        <table>
            <tr><td><input type="submit" onClick="saveForm()" value="Save" /></td>
                <td><input type="submit" onClick="resetForm()" value="Reset" /></td>
                <td><input type="submit" onClick="queryForm()" value="Query" /></td>
                <td><input type="submit" onClick="deleteForm()" value="Delete" /></td></tr>

        </table>
        <div id="film_table">
            <button onclick="displayTable()">Display the film table</button>
        </div>

  </body>
</html>
